package xyz.zhouzhaodong.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;
import xyz.zhouzhaodong.crm.dto.UniversitySpecialtyDto;
import xyz.zhouzhaodong.crm.dto.UniversitySpecialtyDto;
import xyz.zhouzhaodong.crm.entity.UniversitySpecialty;
import xyz.zhouzhaodong.crm.entity.UniversitySpecialty;
import xyz.zhouzhaodong.crm.entity.UniversitySpecialty;
import xyz.zhouzhaodong.crm.mapper.UniversityMapper;
import xyz.zhouzhaodong.crm.mapper.UniversitySpecialtyMapper;
import xyz.zhouzhaodong.crm.mapper.UniversitySpecialtyMapper;
import xyz.zhouzhaodong.crm.service.IUniversitySpecialtyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xyz.zhouzhaodong.crm.util.RestMessage;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 高校-专业表 服务实现类
 * </p>
 *
 * @author zhouzhaodong
 * @since 2020-06-22
 */
@Service
public class UniversitySpecialtyServiceImpl extends ServiceImpl<UniversitySpecialtyMapper, UniversitySpecialty> implements IUniversitySpecialtyService {

    @Resource
    UniversitySpecialtyMapper universitySpecialtyMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage addUniversitySpecialty(UniversitySpecialtyDto universitySpecialtyDto) {
        try {
            UniversitySpecialty universitySpecialty = dtoChange(universitySpecialtyDto);
            String id = UUID.randomUUID().toString().replaceAll("-", "");
            universitySpecialty.setId(id);
            universitySpecialty.setCreateUser(universitySpecialtyDto.getStaffId());
            universitySpecialty.setCreateTime(new Date());
            universitySpecialtyMapper.insert(universitySpecialty);
            return RestMessage.success(id);
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage deleteUniversitySpecialty(UniversitySpecialtyDto universitySpecialtyDto) {
        try {
            UniversitySpecialty universitySpecialty = new UniversitySpecialty();
            universitySpecialty.setId(universitySpecialtyDto.getId());
            universitySpecialty.setUpdateUser(universitySpecialtyDto.getStaffId());
            universitySpecialty.setUpdateTime(new Date());
            universitySpecialty.setFlag(1);
            universitySpecialtyMapper.updateById(universitySpecialty);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage updateUniversitySpecialty(UniversitySpecialtyDto universitySpecialtyDto) {
        try {
            UniversitySpecialty universitySpecialty = dtoChange(universitySpecialtyDto);
            universitySpecialty.setUpdateUser(universitySpecialtyDto.getStaffId());
            universitySpecialty.setUpdateTime(new Date());
            universitySpecialtyMapper.updateById(universitySpecialty);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    public RestMessage selectUniversitySpecialty(UniversitySpecialtyDto universitySpecialtyDto) {
        Integer flag = universitySpecialtyDto.getFlag() == null ? 0 : universitySpecialtyDto.getFlag();
        Page<UniversitySpecialty> page = new Page<>(universitySpecialtyDto.getPageNo(),
                universitySpecialtyDto.getPageSize() == 0 ? 10 :universitySpecialtyDto.getPageSize());
        QueryWrapper<UniversitySpecialty> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("university_id", universitySpecialtyDto.getUniversityId());
        if (universitySpecialtyDto.getDegree() != null && !"".equals(universitySpecialtyDto.getDegree())) {
            queryWrapper.like("degree", universitySpecialtyDto.getDegree());
        }
        if (universitySpecialtyDto.getSpecialty() != null && !"".equals(universitySpecialtyDto.getSpecialty())) {
            queryWrapper.like("specialty", universitySpecialtyDto.getSpecialty());
        }
        if (universitySpecialtyDto.getInstructionLanguage() != null && !"".equals(universitySpecialtyDto.getInstructionLanguage())) {
            queryWrapper.like("instruction_language", universitySpecialtyDto.getInstructionLanguage());
        }
        if (universitySpecialtyDto.getCloseDate() != null && !"".equals(universitySpecialtyDto.getCloseDate())) {
            queryWrapper.like("close_date", universitySpecialtyDto.getCloseDate());
        }
        if (universitySpecialtyDto.getAdmissionSeason() != null && !"".equals(universitySpecialtyDto.getAdmissionSeason())) {
            queryWrapper.eq("admission_season", universitySpecialtyDto.getAdmissionSeason());
        }
        queryWrapper.eq("flag", flag);
        IPage<UniversitySpecialty> list = universitySpecialtyMapper.selectPage(page, queryWrapper);
        return RestMessage.success(list);
    }

    private UniversitySpecialty dtoChange(UniversitySpecialtyDto universitySpecialtyDto) {
        UniversitySpecialty universitySpecialty = new UniversitySpecialty();
        universitySpecialty.setId(universitySpecialtyDto.getId());
        universitySpecialty.setUniversityId(universitySpecialtyDto.getUniversityId());
        universitySpecialty.setSpecialty(universitySpecialtyDto.getSpecialty());
        universitySpecialty.setInstructionLanguage(universitySpecialtyDto.getInstructionLanguage());
        universitySpecialty.setDegree(universitySpecialtyDto.getDegree());
        universitySpecialty.setScholarship(universitySpecialtyDto.getScholarship());
        universitySpecialty.setRemark(universitySpecialtyDto.getRemark());
        universitySpecialty.setFlag(universitySpecialtyDto.getFlag());
        universitySpecialty.setInstructionLanguage(universitySpecialtyDto.getInstructionLanguage());
        universitySpecialty.setCloseDate(universitySpecialtyDto.getCloseDate());
        universitySpecialty.setAdmissionSeason(universitySpecialtyDto.getAdmissionSeason());
        universitySpecialty.setTuition(universitySpecialtyDto.getTuition());
        return universitySpecialty;
    }

}
